package com.idp.web.test.controller;

import com.idp.web.system.entity.SysOrg;
import com.idp.web.test.entity.Logistics;
import com.idp.web.test.entity.LogisticsHasWarehouse;
import com.idp.web.test.entity.Warehouse;
import com.idp.web.test.entity.WarehouseType;
import com.idp.web.test.service.LogisticsHasWarehouseService;
import com.idp.web.test.service.WarehouseService;
import com.idp.web.test.service.WarehouseTypeService;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;

import net.sf.json.JSONObject;

/**
 * 
 * 123123controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 *         <pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {

	private Logger logger = Logger.getLogger(WarehouseController.class);

	@Resource
	private WarehouseService warehouseService;

	@Resource
	private WarehouseTypeService warehouseTypeService;

	@Resource
	private LogisticsHasWarehouseService logisticsHasWarehouseService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {
		request.setAttribute("warehouseType", warehouseTypeService.findBySearch(new WarehouseType()));
		return "shopping/warehouse/storageManage";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param warehouse
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Warehouse warehouse, Page<Warehouse> page, HttpServletRequest request) {
		try {
			request.setAttribute("page", warehouseService.findByPage(warehouse, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/warehouse/storageManage";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/warehouse")
	public String warehouse(Integer id, HttpServletRequest request) {

		try {
			request.setAttribute("warehouseType", warehouseTypeService.findBySearch(new WarehouseType()));
			System.out.println(id);
			System.out.println(ValidateUtils.isNotEmpty(id));
			if (ValidateUtils.isNotEmpty(id)) {
				Warehouse warehouse = warehouseService.getById(id);
				LogisticsHasWarehouse logisticsHasWarehouse = new LogisticsHasWarehouse();
				logisticsHasWarehouse.setWarehouseId(id);
				request.setAttribute("logisticses", logisticsHasWarehouseService.findBySearch(logisticsHasWarehouse));
				request.setAttribute("warehouse", warehouse);
			} else {

			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/warehouse/warehouse";
	}

	@RequestMapping("/getWarehouseInfo")
	public String getWarehouseInfo(Integer id, HttpServletRequest request) {

		try {
			if (ValidateUtils.isNotEmpty(id)) {
				Warehouse warehouse = warehouseService.getById(id);
				request.setAttribute("logisticses", logisticsHasWarehouseService.findByWarehouseId(id));
				request.setAttribute("warehouse", warehouse);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/warehouse/warehouseInfo";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param warehouse
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Warehouse warehouse) {
		JSONObject json = new JSONObject();
		try {
			// 修改
			if (ValidateUtils.isNotEmpty(warehouse.getId())) {
				warehouseService.update(warehouse);
			}
			// 新增
			else {
				warehouse.setId(warehouseService.add(warehouse));
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		json.put("warehouseId", warehouse.getId());
		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		JSONObject json = new JSONObject();
		try {
			warehouseService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}

	/**
	 * 检查仓库是否为空
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/emptyCheck")
	@ResponseBody
	public String emptyCheck(Integer id) {
		JSONObject jsonObject = new JSONObject();
		try {
			if (warehouseService.checkEmpty(id)) {
				jsonObject.put("result", "true");
			} else {
				jsonObject.put("result", "faile");
			}
		} catch (Exception e) {
			jsonObject.put("result", "faile");
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	@RequestMapping("/warehouses")
	@ResponseBody
	public String warehouses(HttpServletRequest request) {
		String jsonStr = "";
		try {
			List<Warehouse> list = warehouseService.findBySearch(new Warehouse());
			jsonStr = JSON.toJSONString(list);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return jsonStr;
	}

	@RequestMapping("/getLogisticses")
	@ResponseBody
	public String getLogisticses(HttpServletRequest request) {
		String id = request.getParameter("warehouseId");
		String json = "";
		try {
			System.out.println(id);
			List<Logistics> logisticses = logisticsHasWarehouseService.findByWarehouseId(Integer.valueOf(id));
			System.out.println(logisticses.size());
			json = JSON.toJSONString(logisticses);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return json;
	}

}
